package com.dingup.provider.aop.classInfoLog;

import com.dingup.api.Util.LoggerUtil;
import com.dingup.api.bean.requestBean.classInfo.ClassInfoBean;
import com.dingup.api.enums.ClassStatusEnum;
import com.dingup.api.enums.ClassUserTypeEnum;
import com.dingup.api.enums.log.LogModelEnum;
import com.dingup.api.enums.log.LogUpdateTypeEnum;
import com.dingup.api.model.classInfo.ClassInfo;
import com.dingup.api.model.classInfo.ClassUserInfo;
import com.dingup.provider.mapper.ClassInfoMapper;
import com.dingup.provider.service.log.SysLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by wancheng on 2018/8/29.
 */
@Component
@Aspect
public class ClassInfoLogAop {

    @Autowired
    ClassInfoMapper classInfoMapper;
    @Autowired
    SysLogService sysLogService;

    @AfterReturning(pointcut = "execution(* com.dingup.provider.service.classInfo.impl.ClassInfoServiceImpl.saveClassInfo(..))",returning="returnValue")
    public void afterSaveLog(JoinPoint joinPoint,ClassInfo returnValue) {
        Object[] obj = joinPoint.getArgs();
        if (obj != null ) {
            ClassInfoBean classInfo = (ClassInfoBean) obj[0];
            sysLogService.saveLog(classInfo.getCreateUserId(), LogUpdateTypeEnum.ADD, "创建了该班级", returnValue.getId(), LogModelEnum.CLASSINFO);
        }
    }
    @Before("execution(* com.dingup.provider.service.classInfo.impl.ClassInfoServiceImpl.deleteClass(..))")
    public void afterDeleteLog(JoinPoint joinPoint) {
        Object[] obj = joinPoint.getArgs();
        if (obj != null) {
            Integer classId = (Integer) obj[0];
            String operaUserId = (String) obj[1];
            ClassInfo classInfo = classInfoMapper.selectByPrimaryKey(classId);
            sysLogService.saveLog(operaUserId, LogUpdateTypeEnum.DELETE, classInfo.getClassName(), classId, LogModelEnum.CLASSINFO);
        }
    }
    @Before("execution(* com.dingup.provider.service.classInfo.impl.ClassInfoServiceImpl.updateClassInfo(..))")
    public void afterUpdateLog(JoinPoint joinPoint) {
        Object[] obj = joinPoint.getArgs();
        if (obj != null ) {
            ClassInfoBean classInfoBean = (ClassInfoBean) obj[0];
            String operaUserId = (String)obj[1];
            ClassInfo classInfo = classInfoBean.getClassInfo();
            ClassInfo older = classInfoMapper.selectByPrimaryKey(classInfo.getId());
            try {
                List<String> content = LoggerUtil.getUpdateAttribute(older, classInfo, ClassInfo.class);
                if (!CollectionUtils.isEmpty(content)) {
                    String contentString = content.stream().collect(Collectors.joining("、 "));
                    sysLogService.saveLog(operaUserId, LogUpdateTypeEnum.UPDATE, contentString, classInfo.getId(), LogModelEnum.CLASSINFO);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
    @Before("execution(* com.dingup.provider.service.classInfo.impl.ClassInfoServiceImpl.updateClassStatus(..))")
    public void jieke(JoinPoint joinPoint) {
        Object[] obj = joinPoint.getArgs();
        if (obj != null) {
            Integer classId = (Integer) obj[0];
            ClassStatusEnum statusEnum = (ClassStatusEnum) obj[1];
            String operaUserId = (String) obj[2];
            if (ClassStatusEnum.CLASS_END.equals(statusEnum)){
                sysLogService.saveLog(operaUserId, LogUpdateTypeEnum.UPDATE_CLASS_END, "结课了该班级", classId, LogModelEnum.CLASSINFO);
            }

        }
    }
    @Before("execution(* com.dingup.provider.service.classInfo.impl.ClassUserInfoServiceImpl.needInsert(..))")
    public void saveClassUserInfoLog(JoinPoint joinPoint) {
        Object[] obj = joinPoint.getArgs();
        if (obj != null ) {
            List<ClassUserInfo> needInsert = (List<ClassUserInfo>) obj[0];
            String operaUserId = (String) obj[1];
            Integer classId = (Integer) obj[2];
            Map<ClassUserTypeEnum,List<ClassUserInfo>> insertMap = needInsert.stream().collect(Collectors.groupingBy(ClassUserInfo::getType));
            List<ClassUserInfo> teachers = insertMap.get(ClassUserTypeEnum.TEACHER);
            List<ClassUserInfo> classTeacher = insertMap.get(ClassUserTypeEnum.CLASSTEACHER);
            List<ClassUserInfo> students = insertMap.get(ClassUserTypeEnum.STUDENT);
            if (!CollectionUtils.isEmpty(teachers)){
                StringBuffer content = new StringBuffer("老师 ");
                teachers.stream().forEach(
                        classUserInfo -> {
                            content.append(classUserInfo.getUserName()+" ");
                        }
                );
                sysLogService.saveLog(operaUserId, LogUpdateTypeEnum.UPDATE_ADD, content.toString(), classId, LogModelEnum.CLASSINFO);
            }
            if (!CollectionUtils.isEmpty(classTeacher)){
                StringBuffer content = new StringBuffer("班主任 ");
                classTeacher.stream().forEach(
                        classUserInfo -> {
                            content.append(classUserInfo.getUserName()+" ");
                        }
                );
                sysLogService.saveLog(operaUserId, LogUpdateTypeEnum.UPDATE_ADD, content.toString(), classId, LogModelEnum.CLASSINFO);
            }
            if (!CollectionUtils.isEmpty(students)){
                StringBuffer content = new StringBuffer("学生 ");
                students.stream().forEach(
                        classUserInfo -> {
                            content.append(classUserInfo.getUserName()+" ");
                        }
                );
                sysLogService.saveLog(operaUserId, LogUpdateTypeEnum.UPDATE_ADD, content.toString(), classId, LogModelEnum.CLASSINFO);
            }
        }
    }

    @Before("execution(* com.dingup.provider.service.classInfo.impl.ClassUserInfoServiceImpl.needDelete(..))")
    public void deleteClassUserInfoLog(JoinPoint joinPoint) {
        Object[] obj = joinPoint.getArgs();
        if (obj != null ) {
            List<ClassUserInfo> needDelete = (List<ClassUserInfo>) obj[0];
            String operaUserId = (String) obj[1];
            Integer classId = (Integer) obj[2];
            if (CollectionUtils.isEmpty(needDelete)) {
                return;
            }
            Map<ClassUserTypeEnum,List<ClassUserInfo>> insertMap = needDelete.stream().collect(Collectors.groupingBy(ClassUserInfo::getType));
            List<ClassUserInfo> teachers = insertMap.get(ClassUserTypeEnum.TEACHER);
            List<ClassUserInfo> classTeacher = insertMap.get(ClassUserTypeEnum.CLASSTEACHER);
            List<ClassUserInfo> students = insertMap.get(ClassUserTypeEnum.STUDENT);
            if (!CollectionUtils.isEmpty(teachers)){
                StringBuffer content = new StringBuffer("老师 ");
                teachers.stream().forEach(
                        classUserInfo -> {
                            content.append(classUserInfo.getUserName()+" ");
                        }
                );
                sysLogService.saveLog(operaUserId, LogUpdateTypeEnum.UPDATE_DELETE, content.toString(), classId, LogModelEnum.CLASSINFO);
            }
            if (!CollectionUtils.isEmpty(classTeacher)){
                StringBuffer content = new StringBuffer("班主任 ");
                classTeacher.stream().forEach(
                        classUserInfo -> {
                            content.append(classUserInfo.getUserName()+" ");
                        }
                );
                sysLogService.saveLog(operaUserId, LogUpdateTypeEnum.UPDATE_DELETE, content.toString(), classId, LogModelEnum.CLASSINFO);
            }
            if (!CollectionUtils.isEmpty(students)){
                StringBuffer content = new StringBuffer("学生 ");
                students.stream().forEach(
                        classUserInfo -> {
                            content.append(classUserInfo.getUserName()+" ");
                        }
                );
                sysLogService.saveLog(operaUserId, LogUpdateTypeEnum.UPDATE_DELETE, content.toString(), classId, LogModelEnum.CLASSINFO);
            }
        }
    }
}
